import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
export default {
	mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件) 
	data() {
		return {
			showDel:false,
			searchVal:'',
			searchLog:[],
			downOption:{
				auto:false
			},
			upOption:{
				auto:false,
				page:{
					num:0,
					size :10,
					noMoreSize:0
				},
				empty:{
					use:false
				}
			},
			tabIndex:0,
			newsTabs:[],
			tempList:[],
			cacheCateList:[],
		}
	},
	methods: {
		useTemp(index){
			var info = this.tempList[index];
			var url = '';
			// switch(info.type)
			// {
			// 	case 1:
			// 		var url = '/pages/form/statistics/add?temp_id='+info.temp_id;
			// 		break;
			// 	case 2:
			// 		var url = '/pages/form/daka/add?temp_id='+info.temp_id;
			// 		break;
			// }
			var url = '/pages/form/statistics/add';
			this.$api.navigateTo(url);
		},
		/*下拉刷新的回调 */
		downCallback() {
			// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
			this.searchVal = '';
			this.getTempCateList();
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback() {
			this.getTempList();
		},
		getTempCateList()
		{
			var that = this;
			that.$http.post('template/getCateList').then(res=>{
				that.newsTabs = res.data.list;
				that.cacheCateList = JSON.parse(JSON.stringify(res.data.list));
				that.tabIndex = 0;
				if(res.code == 1)
				{
					that.getTempList();
				}
			})
		},
		//获取新闻列表
		getTempList(){
			var that = this;
			var info = this.newsTabs[that.tabIndex];
			var current_page = info.current_page?info.current_page:0;
			var last_page = info.last_page?info.last_page:1;
			if(current_page == last_page)
			{
				that.mescroll.endSuccess(that.upOption.page.size,false);
				that.mescroll.showNoMore();return ;
			}
			var localList = info.list?info.list:[];
			var getPage = current_page + 1; //要请求第几页的数据
			that.$http.get('template/getTempList',{params:{
				cate_id:info.cate_id,
				page:getPage,
				per_page:that.upOption.page.size,
				title:that.searchVal
			}}).then(res=>{
				if(res.code == 1)
				{
					var resData = res.data;
					var tempList = localList.concat(resData.data); 
					that.tempList = tempList;
					//把每个下面的数据保存给tab
					info.current_page = resData.current_page;
					info.last_page = resData.last_page;
					info.list = tempList;
					that.newsTabs[that.tabIndex] = info;
					//重置状态
					setTimeout(()=>{
						var haveMore = (info.current_page == info.last_page || resData.data.length == 0)?false:true;
						that.mescroll.endSuccess(resData.data.length,haveMore)
						if(haveMore == false)
						{ 
							that.mescroll.showNoMore();
						}
					},20)
				}else{
					that.tempList = [];
					that.mescroll.endErr();
				}
			}).catch(res=>{
				that.mescroll.endErr();
			});
		},
		// 切换菜单
		tabChange (index) {
			var info = this.newsTabs[index];
			if(!info || index == this.oldtabIndex)
			{
				return ;
			}
			this.oldtabIndex = index;
			this.mescroll.scrollTo(0); //跳转回顶部
			//处理文章数据
			if(info.list && info.list.length >= 0)
			{
				this.tempList = info.list;
				var current_page = info.current_page?info.current_page:0;
				var last_page = info.last_page?info.last_page:1;
				if(current_page == last_page)
				{
					//如果切换的时候 这个tab没更多了 就直接显示没更多内容了
					this.mescroll.showNoMore();
					return ;
				}
				this.mescroll.endSuccess(this.upOption.page.size,true); //否则话设置为还有下一页 
			}else{
				this.getTempList();
			}
		},
		searchList(e)
		{
			var searchVal = e.detail.value?e.detail.value.trim():'';
			this.searchVal = searchVal;
			var searchLog = this.searchLog;
			if(searchVal)
			{
				var index = searchLog.indexOf(searchVal);
				if( index > -1)
				{
					searchLog.splice(index,1);
				}
				
				searchLog.unshift(searchVal)
			}
			if(searchLog.length > 20)
			{
				searchLog.splice(20,1);
			}
			uni.setStorageSync('pages/form/template/index-searchLog',searchLog);
			this.newsTabs = JSON.parse(JSON.stringify(this.cacheCateList))
			//this.tabIndex = 0;
			this.getTempList();
		},
		changeTempLog()
		{
			var searchLog = uni.getStorageSync('pages/form/template/index-searchLog');
			this.searchLog = searchLog?searchLog:[];
		},
		cancelSearch(){
			this.searchVal = '';
			this.newsTabs = JSON.parse(JSON.stringify(this.cacheCateList))
			//this.tabIndex = 0;
			this.getTempList();
		},
		searchCLick(text)
		{
			if(this.showDel == false)
			{
				if(text == this.searchVal) 
				{
					return ;
				}
				this.searchList({
					detail:{
						value:text
					}
				});
			}else{
				this.delLog(text);
			}
		},
		showDelStatus(status)
		{
			var that = this;
			if(status == 'all')
			{
				that.$api.showModal('确认全部删除','',function(res){
					if(res == 'yes')
					{
						that.searchLog = [];
						uni.setStorageSync('pages/form/template/index-searchLog',[]);
						that.showDel = false;
					}
				})
			}else{
				that.showDel = status; 
			}
			
		},
		delLog(text)
		{
			var searchLog = this.searchLog;
			var index = searchLog.indexOf(text);
			if( index > -1)
			{
				this.$api.showModal('确认删除','',function(res){
					if(res == 'yes')
					{
						searchLog.splice(index,1);
						uni.setStorageSync('pages/form/template/index-searchLog',searchLog);
					}
				})
			}
			
		}
	},
	onLoad(){
		this.changeTempLog();
		this.getTempCateList();
	},
	onShow(){
		
	}
}